/**
 * 
 */
package br.gov.sjc.socialalimenta.dao;

import java.util.ArrayList;
import java.util.List;

import br.gov.sjc.classes.GrupoFamiliarTable;
import br.gov.sjc.classes.Nome;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author rodrigo.ramos
 *
 */
public class GrupoFamiliarDAO extends SQLiteOpenHelper{
	private static final String TABELA = "tab_SocialA_GrupoFamiliar";
	private static final String DATABASE_NAME = "SocialAlimenta";
	private static final int VERSION = 1;
	private static final int DATABASE_VERSION = 1;
	private static final String[] COLS = {"_id", "SiasN", "idMembro", "Desc"};
	
	public GrupoFamiliarDAO(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE IF NOT EXISTS " + TABELA + 
		"(_id INTEGER PRIMARY KEY autoincrement," +
		" SiasN INTEGER," + 
		" idMembro INTEGER," +
		" Desc TEXT" + 
		");";
		db.execSQL(sql);
		/**/
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + GrupoFamiliarDAO.TABELA);
		this.onCreate(db);/**/
	}
	
	
	public void inserir(GrupoFamiliarTable gf){ 
		ContentValues valores = new ContentValues();
		valores.put("SiasN", gf.getSiasN());
		valores.put("idMembro", gf.getIdMembro());
		valores.put("Desc", gf.getDesc());

		getWritableDatabase().insert(TABELA, null, valores);
	}
	
	public GrupoFamiliarTable getById(int posicao) {
		Cursor c = getWritableDatabase().query(TABELA, COLS, "_id=?", new String[]{"" + posicao}, null, null, null);
		c.moveToFirst();
		
		GrupoFamiliarTable gf = new GrupoFamiliarTable();
		gf.set_id(c.getInt(0));
		gf.setSiasN(c.getInt(1));
		gf.setIdMembro(c.getInt(2));
		gf.setDesc(c.getString(3));
		
		c.close();
		return gf;
	}
	
	public List<GrupoFamiliarTable> getLista(){
		Cursor c = getWritableDatabase().query(TABELA, COLS, null, null, null, null, null);
		List<GrupoFamiliarTable> lista = new ArrayList<GrupoFamiliarTable>();
		while(c.moveToNext()){
			GrupoFamiliarTable gf = new GrupoFamiliarTable();
			gf.set_id(c.getInt(0));
			gf.setSiasN(c.getInt(1));
			gf.setIdMembro(c.getInt(2));
			gf.setDesc(c.getString(3));
			lista.add(gf);
		}
		c.close();
		return lista;
	}
	
	public void alterar(GrupoFamiliarTable gf) {
		ContentValues valores = new ContentValues();
		valores.put("SiasN", gf.getSiasN());
		valores.put("idMembro", gf.getIdMembro());
		valores.put("Desc", gf.getDesc());
		
		getWritableDatabase().update(TABELA, valores, "_id=?", new String[]{"" + gf.get_id()});
		
	}

	public boolean recreateTable(SQLiteDatabase database) {
		boolean retorno = false;
		try {
			database.execSQL("DROP TABLE IF EXISTS " + TABELA);
	    	onCreate(database);
	    	retorno = true;
		}catch(Exception e){
			Log.e(TABELA,"ERRO MENSAGEM:"+e.getMessage());
        }
		return retorno;
	}
}
